package method;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Tablier {

    private final int n;
    private final int[][] tab;
    private final Set<Integer> ens;
    private final List<Integer> suite;
    private final HashMap<Integer, Integer> map;
    private int max;

    public int getMax() {
        return max;
    }

    public HashMap<Integer, Integer> getMap() {
        return map;
    }

    public int[][] getTab() {
        return tab;
    }

    public Set<Integer> getEns() {
        return ens;
    }

    public int getN() {
        return n;
    }

    public List<Integer> getSuite() {
        return suite;
    }

    public Tablier(int n) {
        this.n = n;
        tab = new int[ n + 1 ][ n + 1 ];
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                tab[i][j] = i * j;
            }
        }

        ens = new TreeSet<Integer>();
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                ens.add(tab[i][j]);
            }
        }
        suite = new ArrayList<Integer>();
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                suite.add(tab[i][j]);
            }
        }
        Iterator<Integer> it = ens.iterator();

        map = new HashMap<Integer, Integer>();
        int cpt = 0;
        max = 0;
        while (it.hasNext()) {
            Integer I = it.next();
            Iterator<Integer> itSuite = suite.iterator();
            while (itSuite.hasNext()) {
                if (itSuite.next() == I) {
                    cpt += 1;
                }
            }
            map.put(I, cpt);
            if (cpt > max) {
                max = cpt;
            }
            cpt = 0;

        }
//        affichage();
    }

    public void affichage() {
        System.out.print(print(ens));
        System.out.println();
        System.out.println(ens.size());
//        System.out.print(map);
        System.out.println();
        System.out.print(print());
        System.out.println();
        System.out.println(printPoids(ens));
    }

    private String printPoids(Set<Integer> ens) {
        String ret = "";
        int res;
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                res = tab[i][j];
                ret += map.get(res) + ",";
            }
            ret += '\n';
        }
        return ret;
    }

    private String print(Set<Integer> ens) {
        String ret = "";
        Iterator<Integer> it = ens.iterator();
        int i = 1;
        while (it.hasNext()) {
            Integer res = it.next();
            if (String.valueOf(res).length() == 1) {
                ret += "00" + res + ",";
            } else if (String.valueOf(res).length() == 2) {
                ret += "0" + res + ",";

            } else {
                ret += res + ",";

            }
            if (i == 8) {
                i = 1;
                ret += '\n';
            } else {
                i++;
            }
        }
        return ret;
    }

    private String print() {
        String ret = "";
        int res;
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                res = tab[i][j];
                if (String.valueOf(res).length() == 1) {

                    ret += "00" + res + "(" + map.get(res) + ")" + ",";

                } else if (String.valueOf(res).length() == 2) {
                    ret += "0" + res + "(" + map.get(res) + ")" + ",";

                } else {
                    ret += res + "(" + map.get(res) + ")" + ",";

                }

            }
            ret += '\n';
        }
        return ret;
    }

    private String repere(int res, String ret) {
        if (ens.contains(res)) {
            ret += "*";
        } else {
            ret += "|";
        }
        return ret;
    }
}
